package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.MD5Util;
import com.itheima.utils.QiNiuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("setMeal")
public class SetMealController {
    @Reference
    private SetmealService setmealService;
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        Jedis jedis = jedisPool.getResource();
        String md5Str;
        try {
            md5Str = MD5Util.md5(imgFile.getBytes());
            Boolean flag = jedis.hexists("imgMD5", md5Str);
            if (flag) {
                String[] strings = jedis.hget("imgMD5", md5Str).split(",");

                return Result.success(MessageConstant.PIC_UPLOAD_SUCCESS, strings);
            }
            String s = imgFile.getOriginalFilename();
            String lastName = s.substring(s.lastIndexOf("."));
            String fileName = UUID.randomUUID().toString() + lastName;
            QiNiuUtil.upload(imgFile.getBytes(), fileName);

            jedis.sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
            jedis.hset("imgMD5", md5Str, fileName + "," + md5Str);

            String[] strings = {fileName, md5Str};

            return Result.success(MessageConstant.PIC_UPLOAD_SUCCESS, strings);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail(MessageConstant.PIC_UPLOAD_FAIL);
        } finally {
            jedis.close();
        }
    }

    @RequestMapping("add")
    public Result add(@RequestBody Setmeal setmeal) {
        return setmealService.add(setmeal);
    }

    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        return setmealService.findPage(queryPageBean);
    }

    @RequestMapping("findById")
    public Result findById(Integer id) {
        return setmealService.findById(id);
    }

    @RequestMapping("update")
    public Result update(@RequestBody Setmeal setmeal) {
        Setmeal s = (Setmeal) setmealService.findById(setmeal.getId()).getData();
        if (s.equals(setmeal)) {
            return Result.success("数据未修改", null);
        }
        return setmealService.update(setmeal);
    }

    @RequestMapping("deleteById")
    public Result deleteById(Integer id) {
        return setmealService.deleteById(id);
    }

    @RequestMapping("findAll")
    public Result findAll() {
        return setmealService.findAll();
    }


}
